package game;


import game.interfaces.AgentInterface;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Vector;

/**
 *@author Gary Doyle, Daniel Collins
 * 
 *Class for the human player object. Contains constructor, betting and IO methods.
 */
public class PlayerAgent implements AgentInterface {
	TileRack rack;
	String currentWord;
    DictionaryTable dt = new DictionaryTable("trimmedList.txt");
    EnglishScrabbleScorer myScorer = new EnglishScrabbleScorer();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    Bag bag = new Bag();
    
    
    
    public int bankRoll, handValue, number, pBet,pScore,counter = 0;
    String word, bestWord, numberIn;
    
    /*
     * Constructor for player agent.
     * @param initial bankroll.
     */
    public PlayerAgent(int br) {
    	rack = new TileRack();
        bankRoll = br;
        currentWord = "";
    }
    
    
    public PlayerAgent(PlayerAgent pa){
    	this.rack = pa.rack;
    	this.bankRoll = pa.bankRoll;
    	this.currentWord = pa.currentWord;
    }
    
    public PlayerAgent(ComputerAgent ca){
    	this.rack = ca.rack;
    	this.bankRoll = ca.bankRoll;
    	this.currentWord = ca.currentWord;
    	this.pScore = ca.cScore;
    }
    
    /*
     * Takes the amount from the agent's bankroll.
     * @param the amount to be subtracted.
     */   
    public void takeFromBankRoll(int amount) {       
            bankRoll = (bankRoll - amount);
    }
    
    /*
     * Adds the amount to the agent's bankroll.
     * @param the amount to be added.
     */
    public void addToBankRoll(int amount) {
        bankRoll = (bankRoll + amount);
    }

    public int incrementCounter(){
    	counter = counter +1;
    	if (counter == 4) counter = 0;
    	return counter;
    }
    
    public boolean isDealer(){
    	return counter == 0;
    }
    
    public boolean isLittleBlind() {
        return counter == 1;
    }
    
    public boolean isBigBlind() {
        return counter == 2;
    }

    /*
     * Check if the player is bankrupt.
     * @param none.
     */
    public boolean isBankrupt() {
        return bankRoll == 0;
    }

    /*
     * Returns the player's bankroll.
     * @param none.
     */   
    public int getCurrentBankRoll() {
        return bankRoll;
    }

    /*
     * Gets the amount to be bet from the user.
     * @param none.
     */
    public int getBetInput() throws IOException {
        try {
            numberIn = br.readLine();
            number = Integer.parseInt(numberIn);

        } catch (NumberFormatException nFE) {
            
            System.out.println("That wasn't a valid number! Please try again!");
            getBetInput();
        }
        return number;
    }

    /*
     * Get the word to be scored from the user.
     * @param none.
     */
    public String getWordInput() {
        try {
            word = br.readLine().toLowerCase();
            if (!dt.containsWord(word)) {
                System.out.println("You didn't enter a valid word. Please try again.");
                getWordInput();
            }
        } catch (IOException e) {
            System.out.println("Error!");
        }
        return word;
    }

    /*
     * Score the word entered by the user.
     * @param none.
     */
    public int getScore(String input) {
        word = input;
        if (dt.containsWord(word)) {
            return handValue = myScorer.StringScore(word);
        }
        
        return handValue;
    }

    /*
     * Generate a vector containing all the valid words which can be made by the player's rack.
     * @param none.
     */
    public Vector<String> validWords(){
    	Vector <String> v = new Vector<String>();

    	for (int i = 2; i <= 8; i++){
    		v.addAll(this.dt.getAllWordsOfLength(i, this.rack.returnString()));
    	}
    	return v;
    }
   
}
